var mongoose = require('../mongoose.js').mongoose;

var SchemaMap = new mongoose.Schema({
    grid    : {
        width   : Number,
        height  : Number
    },

    tiles   :   [],

    //TODO include global vars & set default
    playerAreaBase  :   Number,
    playerRangeBase :   Number,

    defaultTile :   {
        width   :   Number,
        height  :   Number
    }
});

//TODO change to update() / markModified + save() ?
SchemaMap.methods.setGridWH = function(width, height){
    this.grid.width = width;
    this.grid.height = height;
}

SchemaMap.methods.setDefaultTileWH = function(width, height){
    this.defaultTile.width = width;
    this.defaultTile.height = height;
}

SchemaMap.methods.setTiles = function(tiles){
    this.tiles = tiles;
}

var Map = mongoose.model('Map', SchemaMap);

function getMap(callback) {
    Map.find(function(err, maps) {
        if(err) {
            console.log('--- newHero() MAP NOT FOUND: '+err.message+' ---');
            return null;
        }
        else if (maps.length > 0)
            callback(maps[0]);
        else
            callback(null);
    });
}

exports.Map = Map;
exports.getMap = getMap;